package org.activiti.designer.test;

import java.util.List;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.test.Deployment;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml", "classpath:applicationContext-mybatis.xml" })
public class ProcessTestVacationRequest {
	
	@Autowired
	private RuntimeService runtimeService;

	@Autowired
	private TaskService taskService;
	
	@Autowired
	private ProcessEngine processEngine;
	
	@Test
	public void identity(){
		
		IdentityService identityService = processEngine.getIdentityService();
//		User user = identityService.newUser("fozzie");
//		user.setFirstName("fozzie");
//		user.setLastName("fozzie");
//		user.setPassword("pwd");
//		identityService.saveUser(user);
		
		Group group = identityService.newGroup("management");
		group.setName("management");
		group.setType("man");
		identityService.saveGroup(group);
		identityService.createMembership("fozzie", "management");
		
	}
	
	@Test
	@Deployment
	public void deploy(){
		
	}
	
	@Test
	public void startProcessInstance() {
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("financialReport");
		System.out.println(processInstance.getId());
	}
	
	//任务列表
	@Test
	public void taskList(){
		List<Task> userTasks = taskService.createTaskQuery().taskCandidateUser("kermit").list();
		System.out.println("Candidate User kermit Task size:"+userTasks.size());
		List<Task> fozzieTasks = taskService.createTaskQuery().taskCandidateUser("fozzie").list();
		System.out.println("Candidate User fozzie Task size:"+fozzieTasks.size());
		List<Task> groupTasks = taskService.createTaskQuery().taskCandidateGroup("accountancy").list();
		System.out.println("Candidate Group Task size:"+groupTasks.size());
		List<Task> tasks = taskService.createTaskQuery().taskAssignee("fozzie").list();
		System.out.println("Assign task size:"+tasks.size());
	}
	
	//领取任务
	@Test
	public void claim(){
		Task task = taskService.createTaskQuery().taskCandidateGroup("accountancy").singleResult();
		taskService.claim(task.getId(), "fozzie");
		taskList();
	}
	
	@Test
	public void complete(){
		Task task = taskService.createTaskQuery().taskCandidateUser("fozzie").singleResult();
		taskService.complete(task.getId());
	}
	
	@Test
	public void testUserData(){
		
	}

}